import Vue from 'vue'

/**
 * 刷新用户信息
 */
export function refreshUserInfo() {
	
}

/**
 * 显示成功提示框
 */
export function showSuccess(msg, icon, callback) {  
	uni.showToast({
		title: msg,
		icon: icon,
		mask: true,
		duration: 1500,
		success: function() {
			callback && (setTimeout(function() {
				callback();
			}, 1500));
		}
	});
}
Vue.prototype.showSuccess = showSuccess;

/**
 * 显示失败提示框 
 */ 
export function showModal(msg, title, callback, callbackCancel) {  
	uni.showModal({
		title: title ? title : '友情提示',
		content: msg,
		showCancel: true,
		success: function(res) {
			if (res.confirm) {
				callback && callback();
			}else if(res.cancel){
				callbackCancel && callbackCancel();
			}
		}
	});
}
Vue.prototype.showModal = showModal;

/**
 * 生成32位UUID
 */
export function newGuid() {
	var guid = "";
	for (var i = 1; i <= 32; i++) {
		var n = Math.floor(Math.random() * 16.0).toString(16);
		guid += n;
	}
	return guid;
}
Vue.prototype.newGuid = newGuid;

/**
 * 日期转化 月日
 */

export function stringTime(time){
	let date = new Date(time);
	let year = date.getFullYear();
	let month = date.getMonth() + 1; // 月份是从0开始的，所以需要+1
	let day = date.getDate();
	let customFormattedDate = year+'年'+month+'月'+day+'日';
	// let customFormattedDate = date.toLocaleDateString('zh-CN', {
	//     year: 'numeric',
	//     month: 'long',
	//     day: 'numeric'
	// });
	return customFormattedDate;
	
}
Vue.prototype.stringTime = stringTime;
/**
 * 获取当前时间
 */
export function currentTime(time, cFormat) {
	var date = new Date();
	var year = date.getFullYear();
	let month = date.getMonth();
	var dateArr = [
		date.getMonth() + 1,
		date.getDate(),
		date.getHours(),
		date.getMinutes(),
		date.getSeconds(),
	];
	//如果格式是MM则需要此步骤，如果是M格式则此循环注释掉
	for (var i = 0; i < dateArr.length; i++) {
		if (dateArr[i] >= 1 && dateArr[i] <= 9) {
			dateArr[i] = "0" + dateArr[i];
		}
	}
	var strDate =
		year +
		"-" +
		dateArr[0] +
		"-" +
		dateArr[1] +
		" " +
		dateArr[2] +
		":" +
		dateArr[3] +
		":" +
		dateArr[4];
	return strDate
}
Vue.prototype.currentTime = currentTime;


/**
 * 获取当前年月日
 */
export function currentDay(time) {
	var date = new Date(time);
	var year = date.getFullYear();
	let month = date.getMonth();
	var dateArr = [
		date.getMonth() + 1,
		date.getDate()
	];
	//如果格式是MM则需要此步骤，如果是M格式则此循环注释掉
	for (var i = 0; i < dateArr.length; i++) {
		if (dateArr[i] >= 1 && dateArr[i] <= 9) {
			dateArr[i] = "0" + dateArr[i];
		}
	}
	var week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
	var day = date.getDay();    //获取日
	var c = week[day];
	var strDate =
		year +
		"年" +
		dateArr[0] +
		"月" +
		dateArr[1]
		"日" 
	let  times = strDate + ' • ' + c
	return times
}
Vue.prototype.currentDay = currentDay;

/**
 * 时间格式化
 * @param {String} time
 * @param {String} cFormat
 */
export function parseTime(time, cFormat) {
	if (arguments.length === 0) {
	  return null
	}
  if (!time) return ''
  /* 修复IOS系统上面的时间不兼容*/
  if (time.toString().indexOf('-') > 0) {
    time = time.replace(/-/g, '/')
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}
Vue.prototype.parseTime = parseTime;


//一分钟以内刚刚，超一分钟在一小时内的是XX分钟前，超过1小时是在今天的是几点几分
export function timestampFormat( timestamp ) {
	function zeroize( num ) {
		return (String(num).length == 1 ? '0' : '') + num;
	}
	var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
	var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
 
	var curDate = new Date( curTimestamp * 1000 ); // 当前时间日期对象
	var tmDate = new Date( timestamp * 1000 );  // 参数时间戳转换成的日期对象
 
	var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
	var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
 
	if ( timestampDiff < 60 ) { // 一分钟以内
		return "刚刚";
	} else if( timestampDiff < 3600 ) { // 一小时前之内
		return Math.floor( timestampDiff / 60 ) + "分钟前";
	} else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) {
		return  zeroize(H) + ':' + zeroize(i);
	} else {
			return  Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
	}

}
Vue.prototype.timestampFormat = timestampFormat;

//获取链接
export function getUrlParameter(name,url) {
   name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
   var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
   var results = regex.exec(url);
   return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
Vue.prototype.getUrlParameter = getUrlParameter;


